﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DomainDriver.Presentation.Silverlight.Base
{
    public class StateContainer : IStateContainer
    {
        // LATER: Add "event PageRefreshedHandler PageRefreshing;"
        public event PageRefreshedHandler PageRefreshed;
        public event StateChangeHandler StateChanging;
        public event StateChangeHandler StateChanged;

        private IsolatedStorageSettings m_StateCache;

        public StateContainer(IsolatedStorageSettings stateCache)
        {
            m_StateCache = stateCache;
        }

        public void NotifyOfPageRefreshed(string pageUri, object caller)
        {
            if (PageRefreshed == null)
            { return; }

            PageRefreshedEventArgs args = new PageRefreshedEventArgs(pageUri, ChangeType.Changed);
            PageRefreshed(caller, args);
        }

        public bool HasState(string stateName)
        {
            return m_StateCache.Contains(stateName);
        }

        public void AddState(string stateName)
        {
            if (HasState(stateName))
            { throw new InvalidOperationException("The specified state name is already in use."); }

            m_StateCache.Add(stateName, null);
        }

        public void RemoveState(string stateName)
        {
            if (!HasState(stateName))
            { throw new InvalidOperationException("The specified state name does not exist."); }

            m_StateCache.Remove(stateName);
        }

        public object GetStateValue(string stateName)
        {
            if (!HasState(stateName))
            { throw new InvalidOperationException("The specified state name does not exist."); }

            return m_StateCache[stateName];
        }

        public T GetStateValue<T>(string stateName)
        {
            return (T)GetStateValue(stateName);
        }

        public void SetStateValue(string stateName, object stateValue, object caller)
        {
            if (!HasState(stateName))
            { throw new InvalidOperationException("The specified state name does not exist."); }

            StateChangeEventArgs changingEventArgs = new StateChangeEventArgs(stateName, ChangeType.Changing);
            if (StateChanging != null)
            { StateChanging(caller, changingEventArgs); }

            m_StateCache[stateName] = stateValue;

            StateChangeEventArgs changedEventArgs = new StateChangeEventArgs(stateName, ChangeType.Changed);
            if (StateChanged != null)
            { StateChanged(caller, changedEventArgs); }
        }

        public void SetStateValue<T>(string stateName, T stateValue, object caller)
        {
            object stateValueAsObject = (object)stateValue;
            SetStateValue(stateName, stateValueAsObject, caller);
        }
    }
}